/*
 * PID.h
 *
 *  Created on: Jun 10, 2024
 *      Author: Liangmiyi
 */

#ifndef MY_INC_PHY_PHY_SPMW_H_
#define MY_INC_PHY_PHY_SPMW_H_

#include "MY_INC/PHY/PHY_Parameter.h"
#include "stdint.h"

void               Single_Phase_Inverter_SPWM_Alternate(float Vref, uint32_t Pulse_width_HRTIM[2]);
static inline void Single_Phase_Inverter_SPWM_Alternate_Inline(float Vref, float Udc, uint32_t Pulse_width_HRTIM[2])
{
    // 计算相位L的PWM脉冲宽度
    int32_t PhaseA_temp = (int32_t)((Vref / Udc) * PWM_PERIOD);
    // 计算相位N的PWM脉冲宽度
    int32_t PhaseB_temp = PhaseA_temp + PWM_PERIOD;

    // 限制相位L的PWM脉冲宽度在有效范围内
    //    if (PhaseA_temp > (U_LIMIT_HIGH))
    //    {
    //        PhaseA_temp = U_LIMIT_HIGH; // 限制最大值
    //    }
    //    else if (PhaseA_temp < U_LIMIT_ZERO)
    //    {
    //        PhaseA_temp = 0; // 限制最小值
    //    }

    PhaseA_temp = (PhaseA_temp > U_LIMIT_HIGH) ? U_LIMIT_HIGH : PhaseA_temp;
    PhaseA_temp = (PhaseA_temp < U_LIMIT_ZERO) ? 0 : PhaseA_temp;
    PhaseB_temp = (PhaseB_temp > PWM_PERIOD) ? PWM_PERIOD : PhaseB_temp;
    PhaseB_temp = (PhaseB_temp < U_LIMIT_LOW) ? U_LIMIT_LOW : PhaseB_temp;

    //    // 限制相位N的PWM脉冲宽度在有效范围内
    //    if (PhaseB_temp >= PWM_PERIOD)
    //    {
    //        PhaseB_temp = PWM_PERIOD; // 限制最大值
    //    }
    //    else if (PhaseB_temp < (U_LIMIT_LOW))
    //    {
    //        PhaseB_temp = U_LIMIT_LOW; // 限制最小值
    //    }

    // 赋值输出PWM脉冲宽度
    Pulse_width_HRTIM[0] = PhaseA_temp;
    Pulse_width_HRTIM[1] = PWM_PERIOD - PhaseB_temp;
}
void Single_Phase_Inverter_SPWM_FreqDouble(float Vref, float Udc, const uint32_t Period, uint32_t Pulse_width_HRTIM[2]);
// static inline void Single_Phase_Inverter_SPWM_FreqDouble_Inline(float Vref, float Udc, uint32_t Pulse_width_HRTIM[2])
//{
//     // 计算初始PWM脉冲宽度
//     int32_t Vref_temp = (int32_t)((Vref / Udc + 1.0f) * (float)(PWM_PERIOD >> 1));
//     int32_t PhaseA_temp = Vref_temp;              // 计算相位L的PWM脉冲宽度
//     int32_t PhaseB_temp = PWM_PERIOD - Vref_temp; // 计算相位N的PWM脉冲宽度
//
//     // 限制相位L的PWM脉冲宽度在有效范围内
//     if (PhaseA_temp > (U_LIMIT_HIGH))
//         PhaseA_temp = U_LIMIT_HIGH; // 限制最大值
//     else if (PhaseA_temp < U_LIMIT_ZERO)
//         PhaseA_temp = 0; // 限制最小值
//
//     // 限制相位N的PWM脉冲宽度在有效范围内
//     if (PhaseB_temp > (U_LIMIT_HIGH))
//         PhaseB_temp = U_LIMIT_HIGH; // 限制最大值
//     else if (PhaseB_temp < U_LIMIT_ZERO)
//         PhaseB_temp = 0; // 限制最小值
//     // 赋值输出PWM脉冲宽度
//     Pulse_width_HRTIM[0] = PhaseA_temp;
//     Pulse_width_HRTIM[1] = PhaseB_temp;
// }
static inline void Single_Phase_Inverter_SPWM_FreqDouble_Inline(float Vref, float Udc, uint32_t Pulse_width_HRTIM[2])
{
    // 计算初始PWM脉冲宽度
    int32_t PhaseA_temp =
        (int32_t)(((int32_t)(Vref / Udc * (float)PWM_PERIOD) + PWM_PERIOD) >> 1); // 计算相位L的PWM脉冲宽度
    int32_t PhaseB_temp = PWM_PERIOD - PhaseA_temp;                               // 计算相位N的PWM脉冲宽度
    Pulse_width_HRTIM[0] = (PhaseA_temp > U_LIMIT_HIGH) ? U_LIMIT_HIGH : PhaseA_temp;
    Pulse_width_HRTIM[0] = (PhaseA_temp < U_LIMIT_ZERO) ? 0 : PhaseA_temp;
    Pulse_width_HRTIM[1] = (PhaseB_temp > U_LIMIT_HIGH) ? U_LIMIT_HIGH : PhaseB_temp;
    Pulse_width_HRTIM[1] = (PhaseB_temp < U_LIMIT_ZERO) ? 0 : PhaseB_temp;

    // 限制相位L的PWM脉冲宽度在有效范围内
    //    if (PhaseA_temp > (U_LIMIT_HIGH))
    //        PhaseA_temp = U_LIMIT_HIGH; // 限制最大值
    //    else if (PhaseA_temp < U_LIMIT_ZERO)
    //        PhaseA_temp = 0; // 限制最小值

    // 限制相位N的PWM脉冲宽度在有效范围内
    //    if (PhaseB_temp > (U_LIMIT_HIGH))
    //        PhaseB_temp = U_LIMIT_HIGH; // 限制最大值
    //    else if (PhaseB_temp < U_LIMIT_ZERO)
    //        PhaseB_temp = 0; // 限制最小值

    // 赋值输出PWM脉冲宽度
    //    Pulse_width_HRTIM[0] = PhaseA_temp;
    //    Pulse_width_HRTIM[1] = PhaseB_temp;
}
#endif
